<template>
    <div>
        <Breadcrumb :route="route.matched" />
    </div>
    <div class="wb-container">
        <div class="mian-content">
            <div class="deader-content" ref="SearchRef">
                <div class="pull-left">
                    <el-button class="editClick" @click="router.go(-1)">返回详情列表</el-button>
                </div>
            </div>
            <el-table v-loading="loading" element-loading-text="加载数据..." ref="multipleTableRef" :data="goodsList" border
                @selection-change="handleSelectionChange" size="small"
                :style="{ 'height': `calc(100vh - ${mianContentHeight + 180}px)` }"
                :header-cell-style="{ background: '#44abf7', color: '#fff', fontSize: '14px', height: '40px', letterSpacing: '2px' }">
                <template #empty>
                    <el-empty description="暂无数据！" />
                </template>
                <el-table-column type="selection" align="center" />
                <el-table-column prop="goods.name" label="商品名称" align="center" />
                <el-table-column prop="goods.price" label="商品价格" align="center" />
                <el-table-column prop="all_weight" label="总重量" align='center' />
                <el-table-column prop="goods_weight" label="商品重量" align='center' />
                <el-table-column prop="goods.image" label="商品图片" align="center">
                    <template #default="{ row }">
                        <el-tag v-if="Object.keys(row.goods).length == 0">
                            添加图片
                        </el-tag>
                        <el-image style="width: 80px; height: 80px;margin: 10px;" :src="row.goods.image" v-else />
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template #default="{ row }">
                        <el-button type="warning" class="btn-radius" @click="editGoods(row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import { DeviceGoods } from '@/api/Device'
import { DeviceGoodsEdit } from '@/api/Device'
import { jsonFlatten } from '@/utlis/index'
import router from '@/router'
import { useRoute } from "vue-router";
import Breadcrumb from "@/components/nav-header";

const route = useRoute()
const historyParam = history.state.params
const goodsList = ref(null)
const list = reactive({
    form: {
        show_price: '',
        all_weight: '',
        goods_weight: '',
        goods_id: '',
        sales_num: "",
        number: '',
        weight_wave: ''
    }
})

const goodsID = ref()
const loading = ref(true)
const SearchRef = ref()
const mianContentHeight = ref(0)
//获取商品数据
const getGoodsList = async () => {
    const data = await DeviceGoods(historyParam).then(res => {
        const { code, data } = res
        if (code == 200) {
            loading.value = false
            goodsList.value = data
        } else {
            loading.value = false
        }

    }).catch((err) => {
        loading.value = false
    })
}
onMounted(() => {
    mianContentHeight.value = SearchRef.value.clientHeight
    getGoodsList()
})

const dialogFormVisible = ref(false)
const editGoods = (row) => {
    let arr = Object.keys(row.goods)
    let editId = row.id
    let deviceId = row.device_id
    let lockId = row.device_lock_id
    let num = row.number
    let weight = row.goods_weight
    router.push({
        name: 'deviceEdit',
        state: {
            editId,
            lockId,
            num,
            weight,
            deviceId
        }
    })
}
const GoodsEditClick = async (row) => {
    let form = list.form
    let res = JSON.parse(JSON.stringify(form))
    let str = []
    let b = Object.values(res).map(item => String(item))
    let a = Object.keys(res)

    a.map((v, i) => {
        const obj = {};
        obj[v] = b[i];
        str.push(obj);
    });

    let data = { ...str }
    let editForm = jsonFlatten(data)
    let formList = {
        all_weight: editForm.all_weight,
        goods_id: editForm.goods_id,
        goods_weight: editForm.goods_weight,
        sales_num: editForm.sales_num,
        show_price: editForm.show_price
    }
    const edit = await DeviceGoodsEdit(goodsID.value, formList).then(result => {
        const { code, data } = result
        if (code == 200) {
            getGoodsList()
            dialogFormVisible.value = false
        }
    })
}

const handleSelectionChange = () => {

}
</script>
<style lang="less" scoped>
.deader-content {
    width: 100%;
    height: 32px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px 0 20px 20px;
}

.header-right {
    display: flex;
}

.searchinp {
    display: flex;
}

.page-toolbar {
    position: relative;
    width: 100%;
    padding: 0;
    height: 35px;
    line-height: 1;
    vertical-align: middle;
    margin-bottom: 15px;
    display: flex;
    justify-content: space-between;

    .pull-left {
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        align-items: center;

        .el-button {
            margin-left: 10px;
        }
    }

    .btn {
        outline: none !important;
        padding: 8px 16px;
        border-radius: 2px;
        font-size: 12px;
        line-height: 1;
        color: #fff;
        text-align: center;
        vertical-align: middle;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
        cursor: pointer;
        -webkit-transition: background-color 0.3s;
        -moz-transition: background-color 0.3s;
        transition: background-color 0.3s;
        border: 1px solid transparent;
    }

    .btn-primary {
        background: #44abf7 !important;
        border-color: #44abf7 !important;
        color: #fff !important;
    }

    .btn-success {
        background: #54c952 !important;
        border-color: #54c952 !important;
        color: #fff !important;
        margin-left: 0;
    }

    .btn-sm {
        display: inline-block;
        padding: 7px 8px;
        border-radius: 2px;
        font-size: 12px;
        line-height: 1;
        color: #fff;
        background: #dddddd;
        text-align: center;
        vertical-align: middle;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
        cursor: pointer;
        -webkit-transition: background-color 0.3s;
        -moz-transition: background-color 0.3s;
        transition: background-color 0.3s;
    }

    .input-group {
        position: relative;
        display: flex;
        border-collapse: separate;
        display: flex;

        .input-sm {
            height: 30px;
            padding: 5px 10px;
            font-size: 12px;
            line-height: 1.5;
            border-radius: 3px;
        }

        .form-control {
            display: block;
            height: 32px;
            padding: 6px 12px;
            font-size: 14px;
            line-height: 1.42857143;
            color: #555;
            background-color: #fff;
            background-image: none;
            border: 1px solid #ccc;
            border-radius: 4px;
            -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
            box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
            -webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;
            -o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
            transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
        }

        .input-group-btn {
            position: relative;
            font-size: 0;
            white-space: nowrap;
            width: 1%;
            white-space: nowrap;
            vertical-align: middle;
            display: flex;
        }
    }
}

.el-dialog .el-button {
    margin-left: 40px;
    margin-bottom: 20px;
}

:deep(.el-button) {
    padding: 2px 10px;
    font-size: 12px;
    letter-spacing: 2px;
}

.editClick {
    background-color: rgb(212, 215, 222);
    // color: #fff;
}
</style>